<?php

namespace app\api\model;

class UserBankModel extends BaseModel
{
    protected $pk = 'id';
    protected $table = 'hs_user_bank';
    protected static $tableName = 'user_bank';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function info($uid)
    {
        return self::where('user_id', $uid)->find();
    }

    public static function exist($uid)
    {
        return self::where('user_id', $uid)->find();
    }

    public static function saveRecord($params)
    {
        if (isset($params['id'])) {
            //编辑银行卡信息
            $model = UserBankModel::find($params['id']);
            if (!$model) {
                return array('code' => -1, 'msg' => '银行卡记录不存在');
            }
            if ($params['uid'] != $model['user_id']) {
                return array('code' => -1, 'msg' => '银行卡记录不存在');
            }
        } else {
            $model = new self();
        }
        $model->user_id = $params['uid'];
        $model->real_name = isset($params['real_name']) ? $params['real_name'] : '';
        $model->bank = isset($params['bank']) ? $params['bank'] : '';
        $model->bank_card = isset($params['bank_card']) ? $params['bank_card'] : '';
        if (!$model->save()) {
            return array('code' => -1, 'msg' => '添加银行卡失败');
        }
        return array('code' => 1);
    }

    public static function lst($user_id)
    {
        $model = self::where('user_id', $user_id)->field('bank_name,bank_no')->where('hospital_id', 0)->select()->toArray();
        return $model;
    }

    public static function bank_info($hospital_id)
    {
        $model = self::where('hospital_id', $hospital_id)->find();
        if ($model) {
            $model = $model->toArray();
        } else {
            $model = [];
        }
        return $model;
    }
}